/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.persistence.system.hibernate;

import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.springframework.transaction.annotation.Transactional;
import xperiment.metaphor.model.system.Message;
import xperiment.metaphor.persistence.hibernate.HibernateDao;
import xperiment.metaphor.persistence.system.MessageDao;

/**
 * Hibernate Message Data Access Object
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 */
public class HibernateMessageDao extends HibernateDao<Message> implements MessageDao {

    /**
     * Default constructor
     */
    public HibernateMessageDao() {
        super(Message.class);
    }
    
    /**
     * Retrieves the messages by the recipient
     * @param recipientId The id of the recipient
     * @return List<Message>
     */
    @Override
    @Transactional(readOnly=true)
    public List<Message> retrieveByRecipient(Long recipientId) {
       DetachedCriteria detachedCriteria = DetachedCriteria.forClass(getPersistentClass());
       detachedCriteria.add(Expression.eq("recipientId", recipientId));
       detachedCriteria.add(Expression.eq("enabled", true));
       
       return (List<Message>) getHibernateTemplate().findByCriteria(detachedCriteria);
    }

    /**
     * Retrieves the messages by the sender
     * @param senderId The id of the sender of the message
     * @return List<Message>
     */
    @Override
    @Transactional(readOnly=true)
    public List<Message> retrieveBySender(Long senderId) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(getPersistentClass());
       detachedCriteria.add(Expression.eq("senderId", senderId));
       detachedCriteria.add(Expression.eq("enabled", true));
       
       return (List<Message>) getHibernateTemplate().findByCriteria(detachedCriteria);
    }
}
